package com.qfedu.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.pojo.Attr;
import com.qfedu.pojo.Brand;
import com.qfedu.pojo.Phone;
import com.qfedu.service.PhoneService;
import com.qfedu.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PhoneController {

    @Autowired
    private PhoneService phoneService;



    @RequestMapping("/index")
    public String index(Model model,Integer pageNum){
        if (null == pageNum){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,4);

        List<Phone> phones = phoneService.selectAll();
        PageInfo<Phone> pageInfo = new PageInfo<>(phones);
        model.addAttribute("pageInfo",pageInfo);
        List<Brand> strings = phoneService.selectAllBrand();
        model.addAttribute("strings",strings);
        System.out.println(strings);
        return "index";
    }


    @RequestMapping("/brand")
    public String selectAllBrand(Model model){
        List<Brand> strings = phoneService.selectAllBrand();
        model.addAttribute("strings",strings);
//        System.out.println(strings);
        return "index";

    }

    @RequestMapping("/phones/{bid}")
    public String selectAllPhone(@PathVariable int bid,Model model,Integer pageNum){
        if (null == pageNum){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,4);
        List<Brand> strings = phoneService.selectAllBrand();
        model.addAttribute("strings",strings);
        List<Phone> phones = phoneService.selectAllPhone(bid);
        PageInfo pageInfo = new PageInfo(phones);
        model.addAttribute("pageInfo",pageInfo);
       // System.out.println(phones);
        return "index";
    }


    @RequestMapping("/discount/{phoneId}")
    public String selectDiscount(@PathVariable Integer phoneId,Model model){



        //第一步
        List<Attr> attrs = phoneService.selectAllAttr(phoneId,1);
        HashMap<String,List> map = new HashMap<>();
        for (Attr attr : attrs) {
            List<String> list2 = phoneService.selectAttrName(attr.getAttrName());
            map.put(attr.getAttrName(),list2);

        }

        //第二歩
        List<Attr> attrs2 = phoneService.selectAllAttr(phoneId,2);
        HashMap<String,List> map2 = new HashMap<>();
        for (Attr attr : attrs2) {
            List<String> list2 = phoneService.selectAttrName(attr.getAttrName());
            map2.put(attr.getAttrName(),list2);
        }


        //第三步
        List<Attr> attrs3 = phoneService.selectAllAttr(phoneId,3);
        HashMap<String,List> map3 = new HashMap<>();
        for (Attr attr : attrs3) {
            List<String> list3 = phoneService.selectAttrName(attr.getAttrName());
            map3.put(attr.getAttrName(),list3);

        }

        model.addAttribute("map",map);
        model.addAttribute("map2",map2);
        model.addAttribute("map3",map3);
        return "info";
    }


    @RequestMapping("ajax/goods")
    @ResponseBody
    public String price(String[] arr){
        for (String s : arr) {
            System.out.println("a");

        }
        return "price";
    }



}
